<?php


namespace app\common\service\order\coupon_order\jutuike_channel;

use app\model\CouponBrand;
use api\channel\jutuike\ActList;

class Cron
{
    public function cron()
    {
        $exists = CouponBrand::query()->where('belong',  CouponBrand::BELONG_JUTUIKE)
            ->where('status', CouponBrand::STATUS_ACTIVE)
            ->whereNotNull('name')
            ->column('act_id');
        echo '聚推客联盟活动列表-开始' . PHP_EOL;
        $i = 1;
        do {
            $list = (new ActList())->list($i);
            if (!empty($actLists = $list['data'])) {
                foreach ($actLists as $actList) {
                    if (in_array($actList['act_id'], $exists)) {
                        continue;
                    }
                    CouponBrand::findOrCreate([
                        'act_id'    => $actList['act_id'],
                    ],[
                        'act_name'  => $actList['act_name'],
                        'image_url' => $actList['icon'],
                        'status'    => CouponBrand::STATUS_CLOSE,
                        'belong'    =>  CouponBrand::BELONG_JUTUIKE,
                        'ext_data'  => json_encode($actList, JSON_UNESCAPED_UNICODE),
                    ]);
                }
            }
            echo $i.PHP_EOL;
            $i++;
        } while ($list['total'] > 0 && $list['total'] > ($i-1)*100);
        echo '聚推客联盟活动列表-结束' . PHP_EOL;
    }
}